package Algorithm;

import java.util.Stack;

public class detectsymbol {
	public static boolean isValid(String s) {
        Stack stack = new Stack();
        //String tmp ;
        //tmp = String.valueOf(s.charAt(0));
        Character tmp = s.charAt(0);
        if(tmp.equals("]")||tmp.equals("}")||tmp.equals(")")){
                return false;
        }else{
            stack.push(tmp);
        }
        for(int i = 1;i <= s.length()-1;i++){
            tmp = s.charAt(i);
            if(tmp.equals('(')||tmp.equals('[')||tmp.equals('{')){
                stack.push(tmp);
            }else if(tmp.equals(')')){
                //attention :if(stack.peek() != "(" )
                if(stack.empty() || !stack.peek().equals('(')){
                    return false;
                }
                stack.pop();
            }else if(tmp.equals(']')){
                if(stack.empty() || !stack.peek().equals('[')){
                    return false;
                }
                stack.pop();
            }else{
                if(stack.empty() || !stack.peek().equals('{')){
                    return false;
                }
                stack.pop();
            }
        }
        if(!stack.empty()){
            return false;
        }else{
            return true;
        }
    }
	public static void main(String[] args){
		String s = "()[]{}";
		System.out.println(isValid(s));
	}
}
